let s = {
  type: "customAction",
  payload: {
    cid: "c-123-xyz",
    data: '{"name": "打电话"}',
    compData: {
      head: ["地区", "销售额"],
      body: [
        ["东北", 233],
        ["华东", 250],
      ],
    },
    urlParams: {},
  },
};
let s2 = {
  type: "customAction",
  payload: {
    cid: "c-jxdovm3hhaTLReeXdoNFDx",
    data: '{"name": "日期", "type":"StaticTime","minBound":"2023-08-04 00:00:00","maxBound":"2023-08-08 24:00:00"}',
    compData: {},
    urlParams: {
      defaults: {
        "c-jxdovm3hhaTLReeXdoNFDx": {
          type: "StaticTime",
          minBound: "2023-08-04 00:00:00",
          maxBound: "2023-08-08 24:00:00",
        },
      },
    },
  },
};

let sc = {
  type: "customAction",
  payload: {
    cid: "c-jxdovm3hhaTLReeXdoNFDx",
    data: '{"name": "日期", "type":"StaticTime","minBound":"2023-08-04 00:00:00","maxBound":"2023-08-08 24:00:00"}',
    compData: {},
    urlParams: {
      defaults: {
        "c-jxdovm3hhaTLReeXdoNFDx": {
          type: "StaticTime",
          minBound: "2023-08-04 00:00:00",
          maxBound: "2023-08-08 24:00:00",
        },
      },
    },
  },
};
document.addEventListener("click", function (event) {
  if (event.target.innerText.includes("前7天")) {
    let ipt = event.target.querySelector("input");
    if (!ipt) {
      ipt = event.target.parentNode.querySelector("input");
    }
    if (ipt && ipt.checked) ipt.click();
  }
  console.log(event);
});

setTimeout(() => {
  document.addEventListener("click", function (event) {
    if (event.target.innerText.includes("前7天")) {
      let ipt = event.target.querySelector("input");
      if (!ipt) {
        ipt = event.target.parentNode.querySelector("input");
      }
      if (ipt && ipt.checked) ipt.click();
    }
    console.log(event);

    const checkParent = (el, attr, value) => {
      let flag = false;
      if (el.parentNode && el.parentNode.getAttribute(attr) === value) {
        flag = true;
        return flag;
      } else {
        if (el.tagName === "BODY") {
          return flag;
        }
        checkParent(el.parentNode, attr, value);
      }
    };
  });
}, 500)(function () {
  function getScript(url, success) {
    var script = document.createElement("script");
    script.src = url;
    var head = document.getElementsByTagName("head")[0],
      done = false;
    // Attach handlers for all browsers
    script.onload = script.onreadystatechange = function () {
      if (
        !done &&
        (!this.readyState ||
          this.readyState == "loaded" ||
          this.readyState == "complete")
      ) {
        done = true;
        success();
        script.onload = script.onreadystatechange = null;
        head.removeChild(script);
      }
    };
    head.appendChild(script);
  }
  getScript(
    "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js",
    function (res) {
      console.log("res" + res);
    }
  );

  console.log(window);
  console.log($);
  window._$ = window.$;
  console.log(_$);
});

document.addEventListener("click", function (event) {
  if (event.target.innerText.includes("前7天")) {
    let ipt = event.target.querySelector("input");
    if (!ipt) {
      ipt = event.target.parentNode.querySelector("input");
    }
    if (ipt && ipt.checked) ipt.click();
  }

  let quickDate = false; // 快捷时间选择
  const checkParent = (el, attr, value) => {
    if (el.parentNode && el.parentNode.getAttribute(attr) === value) {
      quickDate = true;
    } else if (el.parentNode.tagName !== "BODY") {
      checkParent(el.parentNode, attr, value);
    }
  };
  checkParent(event.target, "data-id", "c-1-665-1932-lkxc4pwc");
  if (quickDate) {
    // 获取URL参数
    function getQueryString(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      var r = window.location.search.substr(1).match(reg);
      if (r != null) {
        return unescape(r[2]);
      }
      return null;
    }
    // 替换指定传入参数的值,paramName为参数,replaceWith为新值
    function replaceParamVal(paramName, replaceWith) {
      var oUrl = this.location.href.toString();
      var re = eval("/(" + paramName + "=)([^&]*)/gi");
      var nUrl = oUrl.replace(re, paramName + "=" + replaceWith);
      this.location = nUrl;
      return nUrl;
    }
    const dateFormat = function (date, fmt = "yyyy-MM-dd hh:mm:ss") {
      if (!date) {
        return date;
      }
      let o = {
        "M+": date.getMonth() + 1, //月份
        "d+": date.getDate(), //日
        "h+": date.getHours(), //小时
        "m+": date.getMinutes(), //分
        "s+": date.getSeconds(), //秒
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        S: date.getMilliseconds(), //毫秒
      };
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(
          RegExp.$1,
          (date.getFullYear() + "").substr(4 - RegExp.$1.length)
        );
      }
      for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
          fmt = fmt.replace(
            RegExp.$1,
            RegExp.$1.length == 1
              ? o[k]
              : ("00" + o[k]).substr(("" + o[k]).length)
          );
        }
      }
      return fmt;
    };
    var today = new Date(new Date().setHours(0, 0, 0, 0));
    let minBound, maxBound;
    if (event.target.innerText.includes("本月")) {
      var firstDay = new Date(today.getFullYear(), today.getMonth(), 1);
      var lastDay = new Date(today.getFullYear(), today.getMonth() + 1, 0);
      minBound = dateFormat(firstDay);
      maxBound = dateFormat(lastDay, "yyyy-MM-dd") + " 24:00:00";
    }
    if (event.target.innerText.includes("昨天")) {
      let lastday = new Date(
        new Date().setHours(0, 0, 0, 0) - 24 * 60 * 60 * 1000
      );
      minBound = dateFormat(lastday);
      maxBound = dateFormat(lastday, "yyyy-MM-dd") + " 24:00:00";
    }
    if (
      event.target.innerText.includes("前7天") ||
      event.target.innerText.includes("前七天")
    ) {
      let lastSevenDay = new Date(
        new Date().setHours(0, 0, 0, 0) - 24 * 60 * 60 * 1000 * 7
      );
      let lastday = new Date(
        new Date().setHours(0, 0, 0, 0) - 24 * 60 * 60 * 1000
      );
      minBound = dateFormat(lastSevenDay);
      maxBound = dateFormat(lastday, "yyyy-MM-dd") + " 24:00:00";
    }
    // 通过集成链接给报告筛选器传递值
    if (minBound && maxBound) {
      let payload = {
        "c-jxdovm3hhaTLReeXdoNFDx": {
          type: "StaticTime",
          minBound: minBound,
          maxBound: maxBound,
        },
      };
      let defaults = getQueryString("defaults");
      let url = location.href;
      let encodeUrl = encodeURIComponent(JSON.stringify(payload));
      if (defaults) {
        url = replaceParamVal("defaults", encodeUrl);
      } else {
        if (url.includes("?")) {
          url += `&defaults=${encodeUrl}`;
        } else {
          url += `?defaults=${encodeUrl}`;
        }
      }
      location.replace(url);
    }
  }
});
setTimeout(() => {
  console.log(document);

  if (document.querySelector('[data-id="c-jxdovm3hhaTLReeXdoNFDx"]')) {
    // 时间筛选器id c-jxdovm3hhaTLReeXdoNFDx
    document.addEventListener("click", function (event) {
      if (event.target.innerText.includes("前7天")) {
        let ipt = event.target.querySelector("input");
        if (!ipt) {
          ipt = event.target.parentNode.querySelector("input");
        }
        if (ipt && ipt.checked) ipt.click();
      }
      function checkParentNode(el, attr, value) {
        if (el.parentNode && el.parentNode.getAttribute(attr) === value) {
          return true;
        } else if (el.parentNode.tagName !== "BODY") {
          return checkParentNode(el.parentNode, attr, value);
        } else if (el.parentNode.tagName === "BODY" || !el.parentNode) {
          return false;
        }
      }
      // 快捷时间选择块id c-1-665-1932-lkxc4pwc
      let quickDate = checkParentNode(
        event.target,
        "data-id",
        "c-1-665-1932-lkxc4pwc"
      ); // 快捷时间选择 判断

      if (quickDate) {
        // 获取URL参数
        function getQueryString(name) {
          var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
          var r = window.location.search.substr(1).match(reg);
          if (r != null) {
            return unescape(r[2]);
          }
          return null;
        }
        // 替换指定传入参数的值,paramName为参数,replaceWith为新值
        function replaceParamVal(paramName, replaceWith) {
          var oUrl = this.location.href.toString();
          var re = eval("/(" + paramName + "=)([^&]*)/gi");
          var nUrl = oUrl.replace(re, paramName + "=" + replaceWith);
          return nUrl;
        }
        const dateFormat = function (date, fmt = "yyyy-MM-dd hh:mm:ss") {
          if (!date) {
            return date;
          }
          let o = {
            "M+": date.getMonth() + 1, //月份
            "d+": date.getDate(), //日
            "h+": date.getHours(), //小时
            "m+": date.getMinutes(), //分
            "s+": date.getSeconds(), //秒
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度
            S: date.getMilliseconds(), //毫秒
          };
          if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(
              RegExp.$1,
              (date.getFullYear() + "").substr(4 - RegExp.$1.length)
            );
          }
          for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
              fmt = fmt.replace(
                RegExp.$1,
                RegExp.$1.length == 1
                  ? o[k]
                  : ("00" + o[k]).substr(("" + o[k]).length)
              );
            }
          }
          return fmt;
        };
        var today = new Date(new Date().setHours(0, 0, 0, 0));
        let minBound, maxBound;
        if (event.target.innerText.includes("本月")) {
          var firstDay = new Date(today.getFullYear(), today.getMonth(), 1);
          var lastDay = new Date(today.getFullYear(), today.getMonth() + 1, 0);
          minBound = dateFormat(firstDay);
          maxBound = dateFormat(lastDay, "yyyy-MM-dd") + " 24:00:00";
        }
        if (event.target.innerText.includes("昨天")) {
          let lastday = new Date(
            new Date().setHours(0, 0, 0, 0) - 24 * 60 * 60 * 1000
          );
          minBound = dateFormat(lastday);
          maxBound = dateFormat(lastday, "yyyy-MM-dd") + " 24:00:00";
        }
        if (
          event.target.innerText.includes("前7天") ||
          event.target.innerText.includes("前七天")
        ) {
          let lastSevenDay = new Date(
            new Date().setHours(0, 0, 0, 0) - 24 * 60 * 60 * 1000 * 7
          );
          let lastday = new Date(
            new Date().setHours(0, 0, 0, 0) - 24 * 60 * 60 * 1000
          );
          minBound = dateFormat(lastSevenDay);
          maxBound = dateFormat(lastday, "yyyy-MM-dd") + " 24:00:00";
        }
        // 通过集成链接给报告筛选器传递值
        if (minBound && maxBound) {
          let payload = {
            "c-jxdovm3hhaTLReeXdoNFDx": {
              type: "StaticTime",
              minBound: minBound,
              maxBound: maxBound,
            },
          };
          let defaults = getQueryString("defaults");
          let url = location.href;
          let encodeUrl = encodeURIComponent(JSON.stringify(payload));
          if (defaults) {
            url = replaceParamVal("defaults", encodeUrl);
          } else {
            if (url.includes("?")) {
              url += `&defaults=${encodeUrl}`;
            } else {
              url += `?defaults=${encodeUrl}`;
            }
          }
          location.replace(url);
        }
      }
    });
  }
}, 1000);

setTimeout(() => {
  if (document.querySelector('[data-id="c-jxdovm3hhaTLReeXdoNFDx"]')) {
    // 1、监听时间筛选器
    document.addEventListener("click", function (event) {
      if (event.target.innerText.includes("前7天")||event.target.innerText.includes("前14天")|| event.target.innerText.includes("前28天")) {
        let ipt = event.target.querySelector("input");
        if (!ipt) {
          ipt = event.target.parentNode.querySelector("input");
        }
        if (ipt && ipt.checked) ipt.click();
      }
    })

    // 2、增加快捷选择器
    const timeEl = document.querySelector('[data-id="c-jxdovm3hhaTLReeXdoNFDx"]');
    const timeTop = timeEl.offsetTop;

    const quickEl = document.createElement("div");
    quickEl.classList.add("quick--box");
    quickEl.style.position = "absolute";
    quickEl.style.top = `${timeTop - 40 }px`;
    quickEl.style.zIndex = "19";
    quickEl.style.fontSize = "20";
    quickEl.style.fontWeight = "600";
    quickEl.style.padding = "0 10px";
    quickEl.style.display = "flex";

    const quickList = ['本月', '昨天', '前7天']
    quickList.forEach(item=>{
      const childDiv = document.createElement("div");
      childDiv.classList.add("quick--tab");
      childDiv.innerText = item;
      childDiv.style.padding = "8px 20px";
      childDiv.style.cursor = "pointer";
      quickEl.appendChild(childDiv);
    })

    // 监听嵌套Div组件的点击事件
    quickEl.addEventListener('click', (event) =>  {
        // 获取URL参数
        function getQueryString(name) {
          var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
          var r = window.location.search.substr(1).match(reg);
          if (r != null) {
            return unescape(r[2]);
          }
          return null;
        }
        // 替换指定传入参数的值,paramName为参数,replaceWith为新值
        function replaceParamVal(paramName, replaceWith) {
          var oUrl = this.location.href.toString();
          var re = eval("/(" + paramName + "=)([^&]*)/gi");
          var nUrl = oUrl.replace(re, paramName + "=" + replaceWith);
          return nUrl;
        }
        const dateFormat = function (date, fmt = "yyyy-MM-dd hh:mm:ss") {
          if (!date) {
            return date;
          }
          let o = {
            "M+": date.getMonth() + 1, //月份
            "d+": date.getDate(), //日
            "h+": date.getHours(), //小时
            "m+": date.getMinutes(), //分
            "s+": date.getSeconds(), //秒
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度
            S: date.getMilliseconds(), //毫秒
          };
          if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(
              RegExp.$1,
              (date.getFullYear() + "").substr(4 - RegExp.$1.length)
            );
          }
          for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
              fmt = fmt.replace(
                RegExp.$1,
                RegExp.$1.length == 1
                  ? o[k]
                  : ("00" + o[k]).substr(("" + o[k]).length)
              );
            }
          }
          return fmt;
        };
        var today = new Date(new Date().setHours(0, 0, 0, 0));
        let minBound, maxBound;
        if (event.target.innerText.includes("本月")) {
          var firstDay = new Date(today.getFullYear(), today.getMonth(), 1);
          var lastDay = new Date(today.getFullYear(), today.getMonth() + 1, 0);
          minBound = dateFormat(firstDay);
          maxBound = dateFormat(lastDay, "yyyy-MM-dd") + " 24:00:00";
        }
        if (event.target.innerText.includes("昨天")) {
          let lastday = new Date(
            new Date().setHours(0, 0, 0, 0) - 24 * 60 * 60 * 1000
          );
          minBound = dateFormat(lastday);
          maxBound = dateFormat(lastday, "yyyy-MM-dd") + " 24:00:00";
        }
        if (
          event.target.innerText.includes("前7天") ||
          event.target.innerText.includes("前七天")
        ) {
          let lastSevenDay = new Date(
            new Date().setHours(0, 0, 0, 0) - 24 * 60 * 60 * 1000 * 7
          );
          let lastday = new Date(
            new Date().setHours(0, 0, 0, 0) - 24 * 60 * 60 * 1000
          );
          minBound = dateFormat(lastSevenDay);
          maxBound = dateFormat(lastday, "yyyy-MM-dd") + " 24:00:00";
        }
        // 通过集成链接给报告筛选器传递值
        if (minBound && maxBound) {
          let payload = {
            "c-jxdovm3hhaTLReeXdoNFDx": {
              type: "StaticTime",
              minBound: minBound,
              maxBound: maxBound,
            },
          };
          let defaults = getQueryString("defaults");
          let url = location.href;
          let encodeUrl = encodeURIComponent(JSON.stringify(payload));
          if (defaults) {
            url = replaceParamVal("defaults", encodeUrl);
          } else {
            if (url.includes("?")) {
              url += `&defaults=${encodeUrl}`;
            } else {
              url += `?defaults=${encodeUrl}`;
            }
          }
          location.replace(url);
        }
      }
    );
    const timeParentEl = timeEl.parentNode;
    timeParentEl.insertBefore(quickEl, timeEl);
  }
},500)
